﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.WeiXin;
using Repository.WeiXinProject;
using Xproject.Util;

namespace Services.WeiXinProject
{
    public class WeiXinServices : Service
    {
        IRepositoryAsync<WeiXinToken> m_WeiXinTokenRepository;

        public WeiXinServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_WeiXinTokenRepository = m_UnitOfWork.RepositoryAsync<WeiXinToken>();
        }
       
        /// <summary>
        /// 得到weixin token
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public WeiXinToken GetWeiXinToken(string appId, string siteName)
        {
            return m_WeiXinTokenRepository.GetWeiXinToken(appId, siteName);
        }
        /// <summary>
        /// 变更weixin token
        /// </summary>
        /// <param name="tokenValue"></param>
        /// <param name="createTime"></param>
        /// <param name="ExprieTime"></param>
        /// <param name="state"></param>
        /// <param name="source"></param>
        /// <param name="id"></param>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public bool UpdateWeiXinToken(string tokenValue, DateTime createTime, DateTime ExprieTime, int state, string source, string id, string siteName)
        {
             WeiXinToken token =  m_WeiXinTokenRepository.Get(id);
            if (token != null)
            {
                token.TokenValue = tokenValue;
                token.CreateTime = createTime;
                token.ExprieTime = ExprieTime;
                token.State = state;
                token.Source = source;
                token.SiteName = siteName;
                token.EntityState = EntityState.Modified;
                m_WeiXinTokenRepository.Update(token);
                return m_UnitOfWork.SaveChanges()==1;
            }
            return false;
            
        }
        /// <summary>
        /// 删除weiXintoken
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public   bool DeleteWeiXinToken(string id)
        {
            WeiXinToken token = m_WeiXinTokenRepository.Get(id);
            if (token != null)
            {
                token.EntityState = EntityState.Deleted;
                m_WeiXinTokenRepository.Delete(token);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tokenValue"></param>
        /// <param name="source"></param>
        /// <param name="id"></param>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public bool InsertWeiXinToken(string tokenValue,string source, string id, string siteName)
        {
            WeiXinToken token = new WeiXinToken();
            token.AppID = id;
            token.CreateTime =Convert.ToDateTime( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            token.ExprieTime = Convert.ToDateTime(DateTime.Now.AddHours(2).ToString("yyyy-MM-dd HH:mm:ss")); 
            token.SiteName = siteName;
            token.Source = source;
            token.State = 1;
            token.TokenValue = tokenValue;
            token.EntityState = EntityState.Added;
            m_WeiXinTokenRepository.Insert(token);
            return m_UnitOfWork.SaveChanges() == 1;
            
        }
    }
}
